<template>
	<view class="wrap">
		<view class="content">
			<view class="title">欢迎注册图书商城</view>
			<!-- 昵称 -->
			<u-input class="u-border-bottom" type="text" v-model="name" placeholder="请输入昵称" />
			<!-- 邮箱 -->
			<u-input class="u-border-bottom" type="text" v-model="email" placeholder="请输入邮箱" />
			<!-- 密码 -->
			<u-input class="u-border-bottom" type="password" v-model="password" placeholder="请输入密码" :password-icon="passwordIcon" />
			<!-- 确认密码 -->
			<u-input class="u-border-bottom" type="password" v-model="password_confirmation" placeholder="请确认密码" :password-icon="passwordIcon" />
			<!-- 注册按钮 -->
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">注册</button>
			<view class="alternative">
				<view class="password">找回密码</view>
				<view class="issue">立即登录</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				passwordIcon: true,
				name: '',
				email: '',
				password: '',
				password_confirmation: ''
			}
		},
		async onLoad() {

		},
		computed: {
			inputStyle() {
				let style = {};
				if(this.name && this.$u.test.email(this.email) && this.password) {
					style.color = "#fff";
					style.backgroundColor = this.$u.color['warning'];
				}
				return style;
			}
		},
		methods: {
			/*
			 * 执行注册
			 */
			async submit() {
				// 1. 表单验证
				if(!this.$u.test.rangeLength(this.$u.trim(this.name), [3, 12])) {
					this.$u.toast('昵称长度为3-12个字符');
					return false ;
				}
				if(!this.$u.test.email(this.$u.trim(this.email))){
					this.$u.toast('邮箱不符合规范');
					return false;
				}
				if(!this.$u.test.rangeLength(this.$u.trim(this.password), [6, 18])) {
					this.$u.toast('密码长度为6-18个字符');
					return false;
				}
				if(this.$u.trim(this.password) != this.$u.trim(this.password_confirmation) ) {
					this.$u.toast('两次密码输入不一致');
					return false;
				}
				
				
				// 2. 准备提交参数
				const params = {
					name: this.name,
					email: this.email,
					password: this.password,
					password_confirmation: this.password_confirmation
				}

				// 3. 请求API，执行注册
				const res = await this.$u.api.authRegister(params);
				 console.log(res);
				// 4. 注册成功之后，重定向到登录(redirect)
				this.$u.toast('注册成功');
				setTimeout(() => {
					this.$u.route({
						type: 'reLaunch', //redirectTo 跳转失败
						url: 'pages/index/index'
					})
				}, 1500)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-border-bottom{
		margin-bottom: 40rpx !important;
	}
	.wrap {
		font-size: 28rpx;

		.content {
			width: 600rpx;
			margin: 80rpx auto 0;

			.title {
				text-align: left;
				font-size: 60rpx;
				font-weight: 500;
				margin-bottom: 100rpx;
			}

			input {
				text-align: left;
				margin-bottom: 10rpx;
				padding-bottom: 6rpx;
			}

			.tips {
				color: $u-type-info;
				margin-bottom: 60rpx;
				margin-top: 8rpx;
			}

			.getCaptcha {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
			}
		}
	}
</style>
